package com.yaoxun.bored.service.sys.authority;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.yaoxun.bored.common.BaseService;
import com.yaoxun.bored.mapper.sys.authority.AuthorityMapper;
import com.yaoxun.bored.model.sys.authority.Authority;
import com.yaoxun.bored.model.sys.authority.RoleConfig;

@Service
public class AuthorityService extends BaseService<Authority> {

	@Inject
	private AuthorityMapper authorityMapper;
	
	public List<Authority> selectMenu() {
		return authorityMapper.selectMenu();
	}
	
	public List<Authority> selectAuthByMenuId(Integer menuId) {
		return authorityMapper.selectAuthByMenuId(menuId);
	}
	
	public void config(RoleConfig config) {
		authorityMapper.deleteRoleAuth(config.getRoleId());
		if(config.getMenuIds() != null && config.getMenuIds().length > 0) {			
			authorityMapper.insertRoleMenu(config);
		}
		if(config.getAuthIds() != null && config.getAuthIds().length > 0) {
			authorityMapper.insertRoleAuth(config);
		}
	}

	public RoleConfig getRoleConfig(Integer roleId) {
		Integer[] menuIds = authorityMapper.selectRoleMenu(roleId);
		Integer[] authIds = authorityMapper.selectRoleAuth(roleId);
		RoleConfig config = new RoleConfig(roleId, menuIds, authIds);
		return config;
	}
	
}
